//
//  WSOrderMapView.m
//  FD
//
//  Created by xiaofang wang on 2018/10/31.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSOrderMapView.h"
#import <GoogleMaps/GoogleMaps.h>     //显示地图
#import <GooglePlaces/GooglePlaces.h> //大头针

//websocket 下载骑手坐标
#import "FDOrderDetailWebSocketTool.h"

@interface WSOrderMapView()<GMSMapViewDelegate>
{
    BOOL _firstLocationUpdate;
}
/**Google地图*/
@property (nonatomic, strong) GMSMapView *mapView;
/**1-7*/
@property (nonatomic, strong) FDOrderDetailWebSocketTool *webSocket;
/**创建大头针-店铺*/
@property (nonatomic, strong) GMSMarker *shopLocationMarker;
/**创建大头针-骑手坐标*/
@property (nonatomic, strong) GMSMarker *riderLocationMarker;
/**创建大头针-用户收货地址*/
@property (nonatomic, strong) GMSMarker *receiveAddressMarker;

@end

@implementation WSOrderMapView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView {
    //创建一个定位坐标点
//    GMSCameraPosition *camer = [GMSCameraPosition cameraWithLatitude:23.868 longitude:151.2086 zoom:14];

    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(51.89015, -8.538966);
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:coordinate2D zoom:14];
    
    //大小和view一样大
    _mapView = [GMSMapView mapWithFrame:self.bounds camera:camera];
    _mapView.delegate = self; //设置代理
    //1-11不要定位
//    _mapView.settings.compassButton = YES; //显示指南针
//    _mapView.settings.myLocationButton = YES; //显示定位按钮
    
    //监听GMSMapView 坐标的移动
    [_mapView addObserver:self forKeyPath:@"myLocation" options:(NSKeyValueObservingOptionNew) context:NULL];
    
    
    //添加到self上
    [self addSubview:_mapView];
    
    //2019-2-18不要定位用户当前位置。
    // Ask for My Location data after the map has already been added to the UI.
//    dispatch_async(dispatch_get_main_queue(), ^{
//        self->_mapView.myLocationEnabled = YES;
//    });
    
}



- (void)mapView:(GMSMapView *)mapView didTapMyLocation:(CLLocationCoordinate2D)location {
    NSString *msg = [NSString stringWithFormat:@"lat:%f: lng:%f",location.latitude,location.longitude];
    
    FDLog(@"定位点是：%@",msg);
}


//移除监听
- (void)dealloc {
    [_mapView removeObserver:self forKeyPath:@"myLocation" context:NULL];
}


#pragma mark - KVO update
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (!_firstLocationUpdate) {
        // If the first location update has not yet been received, then jump to that location.
        _firstLocationUpdate = YES;
        CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
        _mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:14];
    }
}

#pragma mark - getter
- (FDOrderDetailWebSocketTool *)webSocket {
    if (!_webSocket) {
        _webSocket = [[FDOrderDetailWebSocketTool alloc] init];

        FDWeakSelf;
        
        //收到骑手坐标回调
        _webSocket.riderPositionBlock = ^(NSNumber * _Nonnull longitude, NSNumber * _Nonnull latidute) {
               FDLog(@"骑手坐标: log:%@ lat:%@",longitude,latidute);
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([latidute floatValue], [longitude floatValue]);
            weakSelf.riderLocationMarker.position = coordinate;
        };
    }
    return _webSocket;
}

- (GMSMarker *)shopLocationMarker {
    if (!_shopLocationMarker) {
        GMSMarker *myLocationMarker1 = [[GMSMarker alloc] init];
        myLocationMarker1.title = @"AWS3xxx";
//        myLocationMarker1.position =
        myLocationMarker1.appearAnimation = kGMSMarkerAnimationPop;
        myLocationMarker1.flat = YES;
        myLocationMarker1.draggable = YES;
        myLocationMarker1.groundAnchor = CGPointMake(0.5, 0.5);
        myLocationMarker1.icon = [UIImage imageNamed:@"mendian"];
        myLocationMarker1.map = self.mapView;
        
        _shopLocationMarker = myLocationMarker1; //让指针指向
    }
    return _shopLocationMarker;
}

- (GMSMarker *)riderLocationMarker {
    if (!_riderLocationMarker) {
        _riderLocationMarker = [[GMSMarker alloc] init];
//        _riderLocationMarker.position =
        _riderLocationMarker.appearAnimation = kGMSMarkerAnimationPop;
        _riderLocationMarker.flat = YES;
        _riderLocationMarker.draggable = YES;
        _riderLocationMarker.groundAnchor = CGPointMake(0.5, 0.5);
        _riderLocationMarker.icon = [UIImage imageNamed:@"当前位置"];
        //关键代码不写,大头针不显示。
        _riderLocationMarker.map  = self.mapView;
    }
    return _riderLocationMarker;
}
- (GMSMarker *)receiveAddressMarker {
    if (!_receiveAddressMarker) {
        _receiveAddressMarker = [[GMSMarker alloc] init];
        _receiveAddressMarker.appearAnimation = kGMSMarkerAnimationPop;
        _receiveAddressMarker.flat = YES;
        _receiveAddressMarker.draggable = YES;
        _receiveAddressMarker.icon = [GMSMarker markerImageWithColor:[UIColor redColor]];
        _receiveAddressMarker.map = self.mapView;
    }
    return _receiveAddressMarker;
}


//将左边转换为coor
#pragma mark - 接口
- (void)shopLocationLng:(NSNumber *)lng lat:(NSNumber *)lat name:(NSString *)name {
    
    FDLog(@"店铺坐标 lng:%@ lat:%@",lng,lat);
    
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]);
    self.shopLocationMarker.title = name;
    self.shopLocationMarker.position = coordinate;
}


/**2019-2-18显示默认收货地址*/
- (void)showReceiveAddressMarkerWithLng:(NSNumber *)lng lat:(NSNumber *)lat detailAddress:(NSString *)detail {
    FDLog(@"收货地址坐标 lng:%@ - lag:%@",lng,lat);
    
    CLLocationCoordinate2D coordinate =  CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]);
    self.receiveAddressMarker.title = detail;
    self.receiveAddressMarker.position = coordinate;
}



//建立、断开连接
- (void)wsorderMapViewEstimateWebSocketConnectionWithOrderId:(NSString *)orderId {
    [self.webSocket setupWebSocketWithOrderId:orderId];
}


- (void)wsOrderMapViewCloseWebSocket {
    [self.webSocket closewebSocket];
}

#pragma mark -2019-1-7显示大头针
@end
